#!/usr/bin/perl
#
#
$doLWG = $ARGV[0];

$senNum=1;
while($line=<STDIN>) {

	print "$senNum\n";
	my $wordNum=1,$line;
	local(%words,%relWords,%word2lwg);
	while($line=<STDIN>) {
		chomp($line);
		if($line=~/^\s*$/) {
			last;
		}
		while($line=~/\(([^\)\(]+)\)/g) {
			my($pos,$word) = split(/\s+/,$1);
			$words{$wordNum}->{'word'} = $word;
			$words{$wordNum}->{'pos'} = $pos;
			$wordNum++;
		}
	}


	while($line=<STDIN>) {
		chomp($line);
		if($line=~/^\s*$/) {
			last;
		}
		$line=~/^([^\(]+)\(.*-([0-9']+),.*-([0-9']+)/;

		my $role=$1;
		my $parent=$2;
		my $child=$3;

		if($role=~/^conj/) {
			undef $relWords{$child}->{'parent'};
			undef $relWords{$child}->{'role'};
		}

		if($relWords{$child}->{'parent'}) {
			# Shared Argument
			$relWords{$child}->{'shared'} = 1;
		}
		else {
			$relWords{$child}->{'parent'} = $parent;
			$relWords{$child}->{'role'} = $role;
		}

		# Check for cyclic relationship
		#if($relWords{$parent}->{'parent'}==$child) {
		my($p);
		if($p=&cycle($parent,$child,\%relWords)) {
			#print "Cycle between $parent and $child,";
			if($role eq "rcmod") {
				#print " $p\'s parent is removed\n";
				undef $relWords{$p}->{'parent'};
				undef $relWords{$p}->{'role'};
			}
			elsif($relWords{$p}->{'role'}=~/^x/) {
				#print " $p\'s parent is removed\n";
				undef $relWords{$p}->{'parent'};
				undef $relWords{$p}->{'role'};
			}
			else {
				#print " $child\'s parent is removed\n";
				undef $relWords{$child}->{'parent'};
				undef $relWords{$child}->{'role'};
			}
		}

		if($role eq "cop") {
		}

		my($c,$p) = ($child,$parent);
		if($child=~/'/) {
			$relWords{$child}->{'word'} = "DUPLICATE";
			$relWords{$child}->{'pos'} = "DUPLICATE";
		}
		else {
			$relWords{$child}->{'word'} = $words{$child}->{'word'};
			$relWords{$child}->{'pos'} = $words{$child}->{'pos'};
		}

		if($parent=~/'/) {
			$relWords{$parent}->{'word'} = "DUPLICATE";
			$relWords{$parent}->{'pos'} = "DUPLICATE";
		}
		else {
			$relWords{$parent}->{'word'} = $words{$parent}->{'word'};
			$relWords{$parent}->{'pos'} = $words{$parent}->{'pos'};
		}
	}

	# Attach the left out words with their parents
	my(@unattchWords) = &checkDiff(\%words,\%relWords);
	foreach $p (@unattchWords) {

		my($candParent,$currParent);
		if($words{$p}->{'pos'} eq "-RRB-") {
			my($t) = $p;
			for($t=$p-1;$t>=0;$t--) {
				if($words{$t}->{'pos'} eq "-LRB-") {
					$currParent = $relWords{$t}->{'parent'};
					last;
				}
			}
		}
		elsif($words{$p}->{'pos'} eq "''") {
			my($t) = $p;
			for($t=$p-1;$t>=0;$t--) {
				if($words{$t}->{'pos'} eq "``") {
					$currParent = $relWords{$t}->{'parent'};
					last;
				}
			}
		}
		elsif($words{$p}->{'pos'} eq "``") {
			my($t) = $p;
			for($t=$p-1;$t>=0;$t--) {
				if($words{$t}->{'pos'} eq "``") {
					$currParent = $relWords{$t}->{'parent'} if($relWords{$t}->{'parent'}<$p);
					last;
				}
			}
		}

		if(!$currParent) {
		if($p<$wordNum-1) {
			$candParent = $p+1;
			while(! defined $relWords{$candParent} && $candParent<$wordNum) {
				$candParent++;
			}

			if($candParent!=$wordNum) {
				while($candParent && $candParent>$p) {
					$currParent = $candParent;
					$candParent = $relWords{$candParent}->{'parent'};
				}
			}
		}

		if($p==$wordNum-1 || $candParent==$wordNum) {
			$candParent = $p-1;
			while(! defined $relWords{$candParent} && $candParent>0) {
				$candParent--;
			}

			while($candParent) {
				$currParent = $candParent;
				$candParent = $relWords{$candParent}->{'parent'};
			}
		}
		}

		$relWords{$p}->{'word'} = $words{$p}->{'word'};
		$relWords{$p}->{'pos'} = $words{$p}->{'pos'};
		$relWords{$p}->{'parent'} = $currParent;
		$relWords{$p}->{'role'} = lc($words{$p}->{'pos'});

		print STDERR $p."/".$words{$p}->{'word'}."/".$words{$p}->{'pos'}."/$currParent ";
		$unattchPOSTags{$words{$p}->{'pos'}}++;
	}
	print STDERR "\n";

	#foreach $key (sort {$a<=>$b} keys %relWords) {
	#		print STDERR $key.":".$relWords{$key}->{'word'}." ";
	#}
	#print STDERR "\n";


	if(! $doLWG) {
		$root = &markRootgetLibin();
		&printLibinFormat($root);
		next;
	}

	# Local-Word Group - Verb Groups and determiners
	#
	foreach $child (sort {$a<=>$b} keys %relWords) {
		if($relWords{$child}->{'role'}=~/^aux/) {
			$parent = $relWords{$child}->{'parent'};
			if($relWords{$parent}->{'tense'}) {
				$relWords{$parent}->{'tense'}.="_";
			}
			$relWords{$parent}->{'tense'}.=$words{$child}->{'word'};
			delete $relWords{$child};
		}
		elsif($relWords{$child}->{'role'} eq "det") {
			$parent = $relWords{$child}->{'parent'};
			$relWords{$parent}->{'det'} = $words{$child}->{'word'};
			delete $relWords{$child};
		}
			
	}
	
	$root = &markRootgetLibin();
	&printLibinFormat($root);
	$senNum++;
}

foreach $key (sort {$unattchPOSTags{$b}<=>$unattchPOSTags{$a}} keys %unattchPOSTags) {
	print STDERR $key."\t".$unattchPOSTags{$key}."\n";
}

sub cycle {
	my($parent) = shift;
	my($child) = shift;
	my($relWordsRef) = shift;

	while($relWordsRef->{$parent}->{'parent'}) {
		if($relWordsRef->{$parent}->{'parent'}==$child) {
			return $parent;
		}
		$parent = $relWordsRef->{$parent}->{'parent'};
	}
	return 0;
}

sub markRootgetLibin {
	# Mark the Root and get Libin's Format
	my $lwgNum=0;
	my($root,@roots);
	foreach $child (sort {$a<=>$b} keys %relWords) {
		$word2lwg{$child} = $lwgNum;
		$parent = $relWords{$child}->{'parent'};

		#print "$parent --> $child\n";

		if(! $parent) {
			$root = $lwgNum;
			push(@roots,$child);
#			$relWords{$child}->{'word'} = $words{$parent}->{'word'};
#			$relWords{$}->{'pos'} = $words{$parent}->{'pos'};
		}
		else {
			push(@{$relWords{$parent}->{'children'}},$child);
		}

		$lwgNum++;
	}

	my($p,$c);
	$p = $roots[$#roots];
	for(my $i=0;$i<$#roots;$i++) {
		$c = $roots[$i];
		$relWords{$c}->{'parent'} = $p;
		push(@{$relWords{$p}->{'children'}},$c);
		#print "other roots = $word2lwg{$c}\n";
	}

	return $p;
}

sub printLibinFormat {
	my $root = $_[0];
	print "root $word2lwg{$root}\n";
	foreach $child (sort {$a<=>$b} keys %relWords) {
		print "#".$word2lwg{$child}." ".$relWords{$child}->{'word'}."\n";	
		print "\tpos: $relWords{$child}->{'pos'}\n";
		#print "\trole: $relWords{$child}->{'role'}\n" if $relWords{$child}->{'role'};
		if($relWords{$child}->{'role'} ne NULL) {
			print "\trole: $relWords{$child}->{'role'}\n";
		}
		else {
			print "\trole: dep\n";
		}
		if($relWords{$child}->{'tense'}) {
			print "\ttense: $relWords{$child}->{'tense'}\_$relWords{$child}->{'pos'}\n";
		}
		elsif($relWords{$child}->{'pos'}=~/^V/) {
			print "\ttense: $relWords{$child}->{'pos'}\n";
		}
		print "\tdet: $relWords{$child}->{'det'}\n" if $relWords{$child}->{'det'};
		foreach $c(sort {$a<=>$b} @{$relWords{$child}->{'children'}}) {
			print "\tatt #$word2lwg{$c}\n";
		}
	}
	print "\n";
}

sub checkDiff {
	my($hash1) = $_[0];
	my($hash2) = $_[1];
	my(@unattch) = ();

	my($key);
	foreach $key (keys %$hash1) {
		if($hash1->{$key}->{'pos'} ne "IN" && $hash1->{$key}->{'pos'} ne "TO" && ! defined $hash2->{$key}) {
			push(@unattch,$key);
		}
	}
	return sort {$a<=>$b} @unattch;
}

